package com.ruitu.mall_98.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.ruitu.mall_98.R;

public class WebviewActivity extends MyBaseActivity {
    protected WebView webview;//用于显示新闻详情

    protected String title = "";
    protected String url = "";

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_webview;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        webview = findViewById(R.id.webview);

        disableSlideBack();
        title = getIntent().getExtras().getString("title");
        url = getIntent().getExtras().getString("url");

        setHeadTitle(title);
        showProgress();
        initWebview(url);
    }

    private void initWebview(String url) {
        webview.getSettings().setJavaScriptEnabled(true);//开启js的支持
        //自适应屏幕
        webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webview.getSettings().setLoadWithOverviewMode(true);
        webview.setHorizontalScrollBarEnabled(false);//水平不显示
        webview.setVerticalScrollBarEnabled(false); //垂直不显示
        webview.setWebViewClient(new WebViewClient() { //调用自身，不调用系统浏览器
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (TextUtils.isEmpty(url)) {
                } else {
                    if (url.startsWith("tmall://")) {

                    } else {
                        view.loadUrl(url);
                    }
                }
                return true;
            }
        });
        webview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                // Activity和Webview根据加载程度决定进度条的进度大小
                // 当加载到100%的时候 进度条自动消失
                if (progress == 100) {
                    hideProgress();
                }
            }
        });
        webview.loadUrl(url);
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }
}
